<template>
<div class="mod-config">
  <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
    <el-form-item>
      <el-input v-model="dataForm.key" placeholder="請輸入品牌名" size="small" clearable></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="getDataList">查詢</el-button>
      <el-button type="primary" plain @click="addOrUpdateHandle()">新增</el-button>
      <el-button type="danger" plain @click="deleteHandle()">刪除</el-button>
    </el-form-item>
  </el-form>
  <el-table
    :data="dataList"
    border
    v-loading="dataListLoading"
    @selection-change=""
    style="width: 100%">
    <el-table-column prop="brandId" label="品牌id"  header-align="center" align="center"> </el-table-column>
    <el-table-column prop="name" label="品牌名称" header-align="center" align="center"> </el-table-column>
    <el-table-column prop="logo" label="品牌logo"  header-align="center" align="center">
      <template slot-scope="scope">
        <img
          style="width: 100px; height: 100px"
          :src="scope.row.logo" >
      </template>
    </el-table-column>
    <el-table-column prop="showStatus" label="显示状态" header-align="center" align="center">
      <template slot-scope="scope">
        <el-switch
          v-model="scope.row.showStatus"
          :active-value = '1'
          :inactive-value = '0'
          active-color="#13ce66"
          inactive-color="#ff4949" @change="updateBrandStatus(scope.row)">
        </el-switch>
      </template>
    </el-table-column>
    <el-table-column prop="descript" label="介绍" header-align="center" align="center"> </el-table-column>
    <el-table-column
      fixed="right"
      label="操作">
      <template slot-scope="scope">
        <el-button @click="updateCategoryHandle(scope.row.brandId)" type="text" size="large">关联分类</el-button>
        <el-button @click="addOrUpdateHandle(scope.row.brandId)" type="text" size="large ">修改</el-button>
        <el-button type="text" size="large" @click="deleteHandle(scope.row.brandId)">刪除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalPage">
    </el-pagination>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
     <relation-dialog v-if="relationDialogVisible" ref="relationDialog"></relation-dialog>
  </div>

</div>
</template>

<script>
  import brandService from '@/api/product/brand.js';
  import RelationDialog from './brand-category-relation-dialog'
  import addOrUpdate from './brand-add-or-update'
  export default {
    components: {addOrUpdate, RelationDialog},
    props: {},
    data() {
      return {
        relationDialogVisible: false,
        dataListLoading: false,
        addOrUpdateVisible: false,
        dataForm: {
          key:''
        },
        page: 1,
        limit: 10,
        totalPage: 0,
        name: '',
        dataList: []
      }
    },
    methods: {
      updateCategoryHandle(id){
        this.relationDialogVisible = true
        let brandId = id ? id : 0
        this.$nextTick( () => {
          this.$refs.relationDialog.init(brandId)
        })
      },
      addOrUpdateHandle(id){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      deleteHandle(id){
        console.log('要删除的id：',id)
        let ids = id ? [id] : [0];
        this.$confirm('确定删除？','提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          brandService.delete(ids).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      getDataList() {
        this.dataListLoading = true
        let query = {page:this.page, limit:this.limit, key:this.dataForm.key}
        brandService.getList(query).then(({data})=> {
          console.log('查询的结果为',data)
          if( data && data.code === 0) {
            let res = data.data
            this.totalPage = res.totalCount
            this.dataList = res.list
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.size = val
        this.getDataList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val
        this.getDataList()
      },
      updateBrandStatus(row) {
        console.log(row)
        let {brandId, showStatus} = row;
        brandService.update({brandId, showStatus}).then(({data})=>{
          this.$message({
            message: '狀態修改成功',
            type: 'success'
          })
        })
      }
    },
    activated() {
      this.getDataList()
    }
  }
</script>

<style scoped>
    export default {
        props: []
    }
</style>
